package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.dto.*;
import com.sky.entity.Museum;
import com.sky.entity.Reservation;
import com.sky.mapper.MuseumMappper;
import com.sky.mapper.ReservationMapper;
import com.sky.result.PageResult;
import com.sky.service.MuseumService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MuseumServiceImpl implements MuseumService {
    @Autowired
    private MuseumMappper museumMappper;
    @Autowired
    private ReservationMapper reservationMapper;

    @Override
    public List<Museum> MuseumList() {
        List<Museum> museumList = museumMappper.museumList();
        return museumList;
    }

    @Override
    public PageResult pageQuery(MuseumPageQueryDTO museumPageQueryDTO) {
        IPage iPage=new Page(museumPageQueryDTO.getPage(),museumPageQueryDTO.getPageSize());
        LambdaQueryWrapper<Museum> lam=new LambdaQueryWrapper<>();
        if(museumPageQueryDTO.getMuseumName()!=null&&museumPageQueryDTO.getMuseumName()!=" "){
            lam.like(Museum::getMuseumName,museumPageQueryDTO.getMuseumName());
        }
        if(museumPageQueryDTO.getAddress()!=null&&museumPageQueryDTO.getAddress()!=" "){
            lam.like(Museum::getAddress,museumPageQueryDTO.getAddress());
        }
        if(museumPageQueryDTO.getOperator()!=null&&museumPageQueryDTO.getOperator()!=" "){
            lam.like(Museum::getOperator,museumPageQueryDTO.getOperator());
        }
        if(museumPageQueryDTO.getStatus()!=null&&museumPageQueryDTO.getStatus()!=3){
            lam.eq(Museum::getStatus,museumPageQueryDTO.getStatus());
        }
        if(museumPageQueryDTO.getMuseumRa()!=null&&museumPageQueryDTO.getMuseumRa()!=0){
            lam.eq(Museum::getMuseumRa,museumPageQueryDTO.getMuseumRa());
        }
        museumMappper.selectPage(iPage,lam);
        long total=iPage.getTotal();
        List<Museum> museumList=iPage.getRecords();
        PageResult pageResult=new PageResult(total,museumList);
        return pageResult;
    }

    @Override
    public List<Reservation> getReservation(String phone) {
        LambdaQueryWrapper<Reservation> lam=new LambdaQueryWrapper<>();
        if(phone!=null&&phone!=""){
            lam.eq(Reservation::getPhone,phone);
        }
        List<Reservation> reservationList = reservationMapper.selectList(lam);
        return reservationList;
    }

    @Override
    public Museum getMuseumByName(String museumName) {
        LambdaQueryWrapper<Museum> lam=new LambdaQueryWrapper<>();
        if(museumName!=null&&museumName!=""){
            lam.eq(Museum::getMuseumName,museumName);
        }
        Museum museum = museumMappper.selectOne(lam);
        return museum;
    }

    @Override
    public void insertReservation(InsertReservationDTO insertReservationDTO) {
        museumMappper.insertReservation(insertReservationDTO);
    }

    @Override
    public void deleteReservation(ReservationDTO reservationDTO) {
        LambdaQueryWrapper<Reservation> lam=new LambdaQueryWrapper<>();
        if(reservationDTO.getPhone()!=null&&reservationDTO.getPhone()!=""){
            lam.eq(Reservation::getPhone,reservationDTO.getPhone());
        }
        if(reservationDTO.getMuseumName()!=null&&reservationDTO.getMuseumName()!=""){
            lam.eq(Reservation::getMuseumName,reservationDTO.getMuseumName());
        }
        reservationMapper.delete(lam);
    }

    @Override
    public Reservation selectReservation(LocalDateTime dateTime,String phone) {
        LambdaQueryWrapper<Reservation> lam=new LambdaQueryWrapper<>();
        if(dateTime!=null){
            lam.eq(Reservation::getResTime,dateTime);
        }
        if(phone!=null&&phone!=""){
            lam.eq(Reservation::getPhone,phone);
        }
        Reservation reservation = reservationMapper.selectOne(lam);
        return reservation;
    }

    @Override
    public void startOrStop(Long status, Long id) {
        LambdaQueryWrapper<Museum> lam=new LambdaQueryWrapper<>();
        Museum museum = new Museum();
        museum.setStatus(status);
        museum.setId(id);
        museumMappper.updateById(museum);
    }

    @Override
    public Museum getMuseumById(Long id) {
        Museum museum = museumMappper.selectById(id);
        return museum;
    }

    @Override
    public void addMuseum(AddMuseumDTO addMuseumDTO) {
        Museum museum = new Museum();
        BeanUtils.copyProperties(addMuseumDTO, museum);
        museumMappper.insert(museum);
    }

    @Override
    public void updateMuseum(UpdateMuseumDTO updateMuseumDTO) {
        Museum museum=new Museum();
        BeanUtils.copyProperties(updateMuseumDTO,museum);
        museumMappper.updateById(museum);
    }

    @Override
    public Museum selectById(Long id) {
        Museum museum= museumMappper.selectById(id);
        return museum;
    }

    @Override
    public void deleteMuseum(Long id) {
        museumMappper.deleteById(id);
    }

}
